import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*; 
public class AnalyzeDir2Mr {

	static Properties dir_props= new Properties();
	static PrintWriter out;
	static PrintWriter outrm;
	static List<String> dirs= new ArrayList<String>();
	public static void main(String[] args) throws Exception{
		dir_props=loadProperties();
		
		out=new PrintWriter("dirs1.log");
		outrm=new PrintWriter("rm1_unsorted.sh");

		File _dir= new File("tmp");
		File[] farr=_dir.listFiles();
		Long mx=Long.MAX_VALUE;
		TreeMap<Long,List<String>> fmap= new TreeMap<Long, List<String>>();
		for(File _f:farr){
			String _name_=_f.getName();
			int _ii=_name_.lastIndexOf("_");
			int _jj=_name_.lastIndexOf(".");
			Long sz=mx-Long.parseLong(_name_.substring(_ii+1,_jj));
			List<String> strs= fmap.get(sz);
			if(strs==null)
				strs=new ArrayList<String>();
			strs.add(_f.getName());
			fmap.put(sz, strs);
		}
		Set<String> _del=new TreeSet<String>();
		Set<String> coveredDirs=new TreeSet<String>();
		Set<Long> keys=fmap.keySet();
		long gain=0;
		TreeMap<Long,String> smp2= new TreeMap<Long, String>();
		for(Long key:keys){
			if(fmap.get(key).size()>1){
				List<String> vals=fmap.get(key);
				List<String> newvals=new ArrayList<String>();
				newvals.addAll(vals);
				for(String val:vals){
					Long _sz=(999999999999999L-Long.parseLong(val.substring(0,15)));
					if(_sz==0)
						continue;
					String _head="=======SIZE="+_sz+"=====leng="+(mx-key)+"============";
					boolean _print_hd=false;
					
					String _val_path=path(val.substring(15));
					int __v=_val_path.lastIndexOf("/");
					List<String> _m=new ArrayList<String>();
					for(int _i=0;_i<newvals.size();_i++){
						String newval=newvals.get(_i);
						if(!newval.equals(val)){
							String _new_val_path=path(newval.substring(15));
							int __n=_new_val_path.lastIndexOf("/");
							String _pn1=_new_val_path.substring(0,__n);
							String _pv1=_val_path.substring(0,__v);
							String _par1=_pn1+" "+_pv1;
							String _par2=_pv1+" "+_pn1;
							if(_del.contains(_pn1)){
								_del.add(_new_val_path);
							}
							if(_del.contains(_pv1)){
								_del.add(_val_path);
							}
							if(coveredDirs.contains(_pn1)){
								coveredDirs.add(_new_val_path);
							}
							if(coveredDirs.contains(_pv1)){
								coveredDirs.add(_val_path);
							}
							if(coveredDirs.contains(_new_val_path)&& coveredDirs.contains(_val_path)){
								continue;
							}
							if(dif.comp(new String[]{"tmp/"+val,"tmp/"+newval})){
									if(!_print_hd){
										_print_hd=true;
									}
									if( !coveredDirs.contains(_new_val_path) && !_del.contains(_val_path)){
										out.println("meld \""+_val_path+"\" \""+_new_val_path+"\"");
										outrm.println("meld \""+_val_path+"\" \""+_new_val_path+"\"");
										outrm.println("rm -rI \""+_new_val_path+"\"");
										_del.add(_new_val_path);
										coveredDirs.add(_new_val_path);
										coveredDirs.add(_val_path);
										String _stt=smp2.get(mx-_sz);
										if(_stt==null){
											_stt="";
										}
										smp2.put(mx-_sz,_stt+"\nread k\necho \"======size="+_sz+"====\"\n"
												+"echo \""+_val_path+"\" \""+_new_val_path+"\"\n"
												+"meld \""+_val_path+"\" \""+_new_val_path+"\"\n"
												+"echo \"remove -"+_new_val_path+"\"\n"
										+"rm -rI \""+_new_val_path+"\"\n");
										gain+=_sz;
									}else if(!coveredDirs.contains(_val_path)  && !_del.contains(_new_val_path)){
										out.println("meld \""+_new_val_path+"\" \""+_val_path+"\"");
										outrm.println("meld \""+_new_val_path+"\" \""+_val_path+"\"");
										outrm.println("rm -rI \""+_val_path+"\"");
										_del.add(_val_path);
										coveredDirs.add(_new_val_path);
										coveredDirs.add(_val_path);
										String _stt=smp2.get(mx-_sz);
										if(_stt==null){
											_stt="";
										}
										smp2.put(mx-_sz,_stt+"\nread k\necho \"======size="+_sz+"====\"\n"
												+"echo \""+_new_val_path+"\" \""+_val_path+"\"\n"
												+"meld \""+_new_val_path+"\" \""+_val_path+"\"\n"
												+"echo \"remove -"+_val_path+"\"\n"
										+"rm -rI \""+_val_path+"\"\n");
										gain+=_sz;
										
									}
								
							}
							else{
							}
						}else{
						}
					}
					
					
				}
			}
		}
		out.println("\ngain="+gain);
		out.close();
		outrm.close();
		

		
		TreeMap<Long,String> smp= new TreeMap<Long, String>();
		BufferedReader br1= new BufferedReader(new InputStreamReader(new FileInputStream("dirs1.log")));
		String line="";
		int __k="=======SIZE=".length();
		long _len=0;
		boolean _cont=false;
		StringBuffer sb= new StringBuffer("");
		String pre="";
		long total=0;
		while((line=br1.readLine())!= null){
			if(line.startsWith("=======SIZE=")){
				if(sb.toString().length()>0){
					String tt=smp.get(_len);
					if(tt==null)
						smp.put(_len, pre+"\n"+sb.toString());
					else
						smp.put(_len, tt+sb.toString());
				}
				_len=Long.parseLong(line.substring(__k,line.indexOf("==",__k)));
				sb= new StringBuffer("");
				pre=line;
			}else if(line.trim().length()==0){
				if(sb.toString().length()>0){
					String tt=smp.get(_len);
					if(tt==null)
						smp.put(_len, pre+"\n"+sb.toString());
					else
						smp.put(_len, tt+sb.toString());
				}
			}
			else if(line.startsWith("meld")){
				total+=_len;
				sb.append(line+"\n");
			}
			
		}
		br1.close();
		
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("dirs1.sort.log")));
		Set<Long> ss= smp.keySet();
		for(Long l:ss){
			bw.write(smp.get(l));
		}
		bw.write("\n\nTOTAL="+total);
		bw.close();

		BufferedWriter bw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("rm1.sh")));
		Set<Long> ss2= smp2.keySet();
		for(Long l:ss2){
			bw2.write(smp2.get(l));
		}
		bw2.close();

		
	}
	
	static private String path(String file){
		int i=file.length();
		int _jj=file.lastIndexOf("_");
		return (String)dir_props.get(file.substring(1,_jj));
		
	}
	

	
	
	public static void storeProperties(Properties props) throws Exception{
		ByteArrayOutputStream bao= new ByteArrayOutputStream();
		props.storeToXML(bao,"modified-"+new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()));
		byte[] barr=bao.toByteArray();
		FileOutputStream fos=new FileOutputStream(".dir.txt");
		ByteArrayInputStream bis= new ByteArrayInputStream(barr);
		Crypto.getInstance().encrypt(bis,fos);
		fos.close();
		bis.close();
		bao.close();

	}
	public static Properties loadProperties() throws Exception{
		ByteArrayOutputStream bao=new ByteArrayOutputStream();
		FileInputStream fis=new FileInputStream(".dir.txt");
		Crypto.getInstance().decrypt(fis, bao);
		byte[] barr=bao.toByteArray();
		ByteArrayInputStream bis= new ByteArrayInputStream(barr);
		Properties props= new Properties();
		props.loadFromXML(bis);
		bao.close();
		fis.close();
		return props;
	}
	public static String encrypt(String str){
		return Crypto.getInstance().encrypt(str);
	}
	public static String decrypt(String str){
		return Crypto.getInstance().decrypt(str);
	}

}
